package com.pzhu.gmall.manage.service.impl;

import java.util.List;
import java.util.UUID;

import com.alibaba.fastjson.JSON;
import com.pzhu.gmall.bean.PmsSkuAttrValue;
import com.pzhu.gmall.bean.PmsSkuImage;
import com.pzhu.gmall.bean.PmsSkuInfo;
import com.pzhu.gmall.bean.PmsSkuSaleAttrValue;
import com.pzhu.gmall.manage.constant.SkuConstant;
import com.pzhu.gmall.manage.mapper.PmsSkuAttrValueMapper;
import com.pzhu.gmall.manage.mapper.PmsSkuImageMapper;
import com.pzhu.gmall.manage.mapper.PmsSkuInfoMapper;
import com.pzhu.gmall.manage.mapper.PmsSkuSaleAttrValueMapper;
import com.pzhu.gmall.service.CacheService;
import com.pzhu.gmall.service.SkuInfoService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 刘鹏 liupeng
 * @date 2019/11/1
 **/
@Service
public class SkuInfoServiceImpl<T> implements SkuInfoService<T> {
    @Autowired
    private PmsSkuInfoMapper pmsSkuInfoMapper;
    @Autowired
    private PmsSkuImageMapper pmsSkuImageMapper;
    @Autowired
    private PmsSkuAttrValueMapper pmsSkuAttrValueMapper;
    @Autowired
    private PmsSkuSaleAttrValueMapper pmsSkuSaleAttrValueMapper;
    @Reference
    private CacheService cacheService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(PmsSkuInfo pmsSkuInfo) {
        //PmsSkuAttrValue PmsSkuImage PmsSkuInfo  PmsSkuSaleAttrValue
        pmsSkuInfoMapper.insertSelective(pmsSkuInfo);
        //存储图片信息
        List<PmsSkuImage> pmsSkuImageList = pmsSkuInfo.getSkuImageList();
        for (PmsSkuImage pmsSkuImage : pmsSkuImageList) {
            pmsSkuImage.setSkuId(pmsSkuInfo.getId());
            pmsSkuImageMapper.insertSelective(pmsSkuImage);
        }
        //存储平台属性信息
        List<PmsSkuAttrValue> pmsSkuAttrValueList = pmsSkuInfo.getSkuAttrValueList();
        for (PmsSkuAttrValue pmsSkuAttrValue : pmsSkuAttrValueList) {
            pmsSkuAttrValue.setSkuId(pmsSkuInfo.getId());
            pmsSkuAttrValueMapper.insertSelective(pmsSkuAttrValue);
        }
        //存储销售数据信息
        List<PmsSkuSaleAttrValue> pmsSkuSaleAttrValueList = pmsSkuInfo.getSkuSaleAttrValueList();
        for (PmsSkuSaleAttrValue pmsSkuSaleAttrValue : pmsSkuSaleAttrValueList) {
            pmsSkuSaleAttrValue.setSkuId(pmsSkuInfo.getId());
            pmsSkuSaleAttrValueMapper.insertSelective(pmsSkuSaleAttrValue);
        }

    }

    @Override
    public PmsSkuInfo getSkuByItemId(String itemId) throws InterruptedException {
        //获得key
        String key = SkuConstant.getSkuRedisKey(itemId);
        //获得 uuid
        String uuid = UUID.randomUUID().toString();
        //获得skuLock
        String lockKey = SkuConstant.getSkuRedisLockKey(itemId);
        //转化为json字符串
        String s = cacheService.getJsonStringByKey(key);
        PmsSkuInfo pmsSkuInfo = null;
        if (StringUtils.isBlank(s)) {
            //防止并发量太高，冲击mysql

            Boolean lock = cacheService.lock(lockKey, uuid, 2L);
            while (!lock) {
                Thread.sleep(2000);
                lock = cacheService.lock(lockKey, uuid, 2L);
            }

            pmsSkuInfo = pmsSkuInfoMapper.selectByItemId(itemId);
            cacheService.unLock(lockKey, uuid);
            if (pmsSkuInfo == null) {
                cacheService.setObjectBySecond(key, JSON.toJSONString(""), 5L);
            } else {
                cacheService.setObjectBySecond(key, pmsSkuInfo, 30L);
            }
            return pmsSkuInfo;
        }
        pmsSkuInfo = JSON.parseObject(s, PmsSkuInfo.class);
        return pmsSkuInfo;
    }

    @Override
    public List<T> getSkuSearchInfo() {
        return pmsSkuInfoMapper.selectSearchInfo();
    }


}
